<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="乘客姓名" prop="passengerName">
        <el-input
          v-model="queryParams.passengerName"
          placeholder="请输入乘客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入下单用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="乘客身份证号" prop="passengerId">
        <el-input
          v-model="queryParams.passengerId"
          placeholder="请输入乘客身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="列车编号" prop="trainId">
        <el-input
          v-model="queryParams.trainId"
          placeholder="请输入列车编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座位号" prop="seatNum">
        <el-input
          v-model="queryParams.seatNum"
          placeholder="请输入座位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否删除" prop="isDel">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDel"-->
<!--          placeholder="请输入是否删除"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:orders:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:orders:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:orders:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="乘客姓名" align="center" prop="passengerName" />
      <el-table-column label="下单用户" align="center" prop="userName" />
      <el-table-column label="乘客身份证号" align="center" prop="passengerId" />
      <el-table-column label="列车编号" align="center" prop="trainId" />
      <el-table-column label="座位号" align="center" prop="seatNum" />
      <el-table-column label="订单状态" align="center" prop="orderStatusRemark" />
      <el-table-column label="发车时间" align="center" prop="orderStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到站时间" align="center" prop="orderEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderEndTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orders:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="success"
            icon="el-icon-bank-card"
            @click="pay(scope.row)"
            v-show="scope.row.orderStatus == '1'"
            v-hasPermi="['system:orders:pay']"
          >支付</el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-close"
            @click="cancelorder(scope.row)"
            v-show="scope.row.orderStatus == '1'"
            v-hasPermi="['system:orders:pay']"
          >取消订单</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-close"
            @click="backorder(scope.row)"
            v-show="scope.row.orderStatus == '2'"
            v-hasPermi="['system:orders:pay']"
          >退票</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orders:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="乘客姓名" prop="passengerName">
          <el-input v-model="form.passengerName" placeholder="请输入乘客姓名" disabled/>
        </el-form-item>
        <el-form-item label="下单用户" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入下单用户" disabled/>
        </el-form-item>
        <el-form-item label="乘客身份证号" prop="passengerId">
          <el-input v-model="form.passengerId" placeholder="请输入乘客身份证号" disabled/>
        </el-form-item>
        <el-form-item label="列车编号" prop="trainId">
          <el-input v-model="form.trainId" placeholder="请输入列车编号" disabled/>
        </el-form-item>
        <el-form-item label="座位号" prop="seatNum">
          <el-input v-model="form.seatNum" placeholder="请输入座位号" disabled/>
        </el-form-item>
        <el-form-item label="状态" prop="orderStatue">
          <el-input v-model="form.orderStatus" placeholder="请输入座位号" disabled/>
        </el-form-item>
<!--        <el-form-item label="是否删除" prop="isDel">-->
<!--          <el-input v-model="form.isDel" placeholder="请输入是否删除" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关闭</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/system/orders";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        passengerName: null,
        userName: null,
        passengerId: null,
        trainId: null,
        seatNum: null,
        orderStatus: null,
        isDel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        passengerName: null,
        userName: null,
        passengerId: null,
        trainId: null,
        seatNum: null,
        orderStatus: "0",
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrders(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单信息";
      });
    },
    pay(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrders(orderId).then(response => {
        this.form = response.data;
        this.form.orderStatus = '2'
        updateOrders(this.form).then(response => {
          this.$modal.msgSuccess("支付成功");
          this.getList();
        });
      });
    },
    // 退票
    backorder(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrders(orderId).then(response => {
        this.form = response.data;
        this.form.orderStatus = '4'
        updateOrders(this.form).then(response => {
          this.$modal.msgSuccess("退票成功.");
          this.getList();
        });
      });
    },
    // 取消订单
    cancelorder(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrders(orderId).then(response => {
        this.form = response.data;
        this.form.orderStatus = '3'
        updateOrders(this.form).then(response => {
          this.$modal.msgSuccess("取消订单成功.");
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrders(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
